// 测试textbooks表查询
const { promiseQuery: query } = require('./db');

async function testTextbooksQuery() {
  try {
    console.log('开始测试textbooks表查询...');
    
    // 测试获取教材列表
    const sql = `
      SELECT t.*, c.name as category_name, u.username as seller_name
      FROM textbooks t
      LEFT JOIN categories c ON t.category_id = c.id
      LEFT JOIN users u ON t.seller_id = u.id
      WHERE t.status = 'on_sale'
      ORDER BY t.created_at DESC
      LIMIT 8
    `;
    
    const result = await query(sql);
    console.log('查询成功！返回的教材数量：', result.length);
    console.log('教材数据示例：', result.length > 0 ? JSON.stringify(result[0], null, 2) : '无数据');
    
    // 测试获取总数
    const countResult = await query('SELECT COUNT(*) as total FROM textbooks WHERE status = "on_sale"');
    console.log('在售教材总数：', countResult[0].total);
    
    console.log('\n✅ 数据库修复成功！textbooks表查询正常工作。');
    
  } catch (error) {
    console.error('\n❌ 查询失败：', error.message);
    console.error('错误详情：', error);
  }
}

// 运行测试
testTextbooksQuery();